import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

class Solution {
    List<List<Integer>> ret = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    boolean[] check;
    public List<List<Integer>> permute(int[] nums) {
        int n = nums.length;
        check = new boolean[n];

        dfs(nums);
        return ret;
    }
    public void dfs(int[] nums){
        // 递归的出口
        if(path.size() == nums.length){
            ret.add(new ArrayList<>(path));
            //List<Integer> tmp = new ArrayList<>(path);
            //ret.add(tmp);
            return;
        }
        for(int i = 0; i < nums.length; i++){
            if(!check[i]){
                path.add(nums[i]);
                check[i] = true;
                dfs(nums);
                check[i] = false;
                path.remove(path.size() - 1);
            }
        }
    }
}